package com.luoqy.speedy.core.base.controller;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.luoqy.speedy.data.MySql;
import com.luoqy.speedy.util.Result;
import com.luoqy.speedy.util.aop.log.Log;

/**
 * 测试管理
 * 同步记录
 *
 * @author lumberer
 * @time 2020-07-28 15:15-00
 */
@Controller
@RequestMapping("/common/record")
public class SyncRecordController {
    private String PREFIX = "WEB-INF/common/record/";

    /**
     *
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "sync_record";
    }

    /**
     *
     */
    @RequestMapping("/addPage")
    public String addPage() {
        return PREFIX + "sync_recordAdd";
    }

    /**
     *
     */
    @RequestMapping("/updatePage")
    public String updatePage(Model model, String id) {
        Object data = MySql.SELECTONE("select * from sync_record where id=" + id, Map.class);
        model.addAttribute("sync_record", data);
        return PREFIX + "sync_recordUpdate";
    }

    /**
     *
     */
    @RequestMapping("/loadPage")
    @ResponseBody
    public Result loadPage(String page, String pageSize) {
        Result result = new Result();
        int pages = 0;
        int pageSizes = 20;
        if (null != pageSize) {
            pageSizes = Integer.parseInt(pageSize);
        }
        if (page != null && pageSize != null && !"".equals(page) && !"".equals(pageSize)) {
            pages = Integer.valueOf(page);
            pageSizes = Integer.valueOf(pageSize);
            pages = pages * pageSizes;
        }
        Object data = MySql.SELECTLIST("select * from sync_record limit " + pages + "," + pageSize,Map.class);
        return Result.success("数据加载成功", data);
    }

    /**
     *
     */
    @RequestMapping("/update")
    @ResponseBody
    public Result update(HttpServletRequest req, String id) {
        Result result = new Result();
        Enumeration<String> parames = req.getParameterNames();
        String set = " set ";
        while (parames.hasMoreElements()) {
            String name = (String) parames.nextElement();//调用nextElement方法获得元素
            String value = req.getParameter(name);
            if (!"id".equals(name)) {
                if (null != value && !"".equals(value)) {
                    set += name + "='" + value + "',";
                }
            }
        }
        if ("set".equals(set.trim())) {
            return Result.error("请传入需要更新的参数");
        } else {
            set = set.substring(0, set.length() - 1);
            int call = MySql.EXECUTE("update sync_record" + set + " where id=" + id);
            if (call > 0) {
                return Result.success("更新成功", null);
            } else {
                return Result.error("更新失败");
            }
        }
    }

    /**
     *
     */
    @RequestMapping("/add")
    @ResponseBody
    @Log({"数据增加操作", ""})
    public Result add(HttpServletRequest req) {
        Enumeration<String> parames = req.getParameterNames();
        String set = " set ";
        while (parames.hasMoreElements()) {
            String name = (String) parames.nextElement();
            String value = req.getParameter(name);
            if (!"id".equals(name)) {
                if (null != value && !"".equals(value)) {
                    set += name + "='" + value + "',";
                }
            }
        }
        if ("set".equals(set.trim())) {
            return Result.error("请传入需要保存的参数");
        } else {
            set = set.substring(0, set.length() - 1);
            int call = MySql.EXECUTE("insert into sync_record" + set);
            if (call > 0) {
                return Result.success("保存成功", null);
            } else {
                return Result.error("保存失败");
            }
        }
    }
}